package com.gsm.phimhanhdong;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Configuration;
import android.util.DisplayMetrics;

public class Utils {

	private static final long LOW_STORAGE_THRESHOLD = 1024 * 1024 * 10;
	public static final String ALDIKO_SLUG = "aldikodbookdreaderdpremium";
	public static final String MOON_SLUG = "moond-reader-pro-tieng-viet";
	public static final String FBREADER_SLUG = "fbreader";
	public static final String ACV_SLUG = "droiddcomicdviewer";
	
	public static ArrayList<String> pullLinks(String text) {
		ArrayList<String> links = new ArrayList<String>();
		String regex = "\\(?\\b(http://|www[.])[-A-Za-z0-9+&@#/%?=~_()|!:,.;]*[-A-Za-z0-9+&@#/%=~_()|]";
		Pattern p = Pattern.compile(regex);
		Matcher m = p.matcher(text);
		while(m.find()) {
			String urlStr = m.group();
			if (urlStr.startsWith("(") && urlStr.endsWith(")")){
				urlStr = urlStr.substring(1, urlStr.length() - 1);
			}
			links.add(urlStr);
		}
		return links;
	}
	
	public static String formatNumber(String pattern, int number){
		DecimalFormat myFormatter = new DecimalFormat(pattern);
	    String output = myFormatter.format(number);
	    return output;
	}
	
	public static long calculateDaysToGetGift(int daysToGet){
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
		c.add(Calendar.DATE, daysToGet);
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
		String time = sdf.format(c.getTime());
		long unixTime = 0;
		try {
			unixTime = sdf.parse(time).getTime() / 1000L;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return unixTime;
	}
	
	public static void launch(Context context, String packageName){
		try {
			Intent launch = context.getPackageManager().getLaunchIntentForPackage(packageName);
			launch.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			context.startActivity(launch);
		} catch(ActivityNotFoundException e) {
			e.printStackTrace();
		}
		
	}
	
	public static String secondsToString(int seconds) {
		String s = String.format("%02d", seconds / 60) + ":";
		int t = seconds % 60;
		s += t < 10 ? "0" + t : t;
		return s;
	}
	
	public static int getWidthScreen(Context context) {
		DisplayMetrics metric = context.getResources().getDisplayMetrics();
		int width = metric.widthPixels;
		return width;
	}

	public static int getHeightScreen(Context context) {
		DisplayMetrics metric = context.getResources().getDisplayMetrics();
		int height = metric.heightPixels;
		return height;
	}
	
	public static String generateRandomString(int len){
		String str = "0123456789abcdefghijklmnopqrstuvwxyz";
		Random rnd = new Random();
		StringBuilder sb = new StringBuilder(len);
		   for( int i = 0; i < len; i++ ) {
			   sb.append(str.charAt( rnd.nextInt(str.length()))); 
		   }
		return sb.toString();
	}

	public static boolean isInteger(String s) {
		try {
			Integer.parseInt(s);
		} catch (NumberFormatException e) {
			return false;
		}
		return true;
	}

	public static boolean isTablet(Context context) {
		return (context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE;
	}

	public static String getInstalledPackageNameListStr(Context context) throws NameNotFoundException {
		String returnList = "";
		PackageManager pm = context.getPackageManager();
		List<PackageInfo> packs = context.getPackageManager().getInstalledPackages(0);
		for (int i = 0; i < packs.size(); i++) {
			PackageInfo p = packs.get(i);
			ApplicationInfo ai = pm.getApplicationInfo(p.packageName, 0);
			String appName = p.applicationInfo.loadLabel(
					context.getPackageManager()).toString();
			if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0
					|| appName.equals("AppVIETTEL")
					|| appName.equals("AppMOBI") || appName.equals("AppVINA")
					|| appName.equalsIgnoreCase("appstorevn")
					|| appName.equalsIgnoreCase("senstore")) {
				continue;
			}
			returnList += "\"" + p.applicationInfo.packageName + "\":\""
					+ p.versionName + "|" + p.versionCode + "\",";
		}
		if (returnList.length() > 0) {
			StringBuilder sb = new StringBuilder(returnList.substring(0,
					returnList.length() - 1));
			sb.insert(0, "{");
			sb.insert(returnList.length(), "}");
			return sb.toString();
		} else {
			return "";
		}
	}

	
}
